package es.webapp.exiquets.domain.entities;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name="users", catalog="exiquetsdb", uniqueConstraints = {
		@UniqueConstraint(columnNames = "mail")
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="user_id")
	private int userId;

	private byte enabled;

	private String mail;

	private String nombre;

	private String password;

	//bi-directional many-to-many association to Producto
	@ManyToMany(mappedBy="users", fetch=FetchType.EAGER)
	private Set<Producto> productos;

	//bi-directional many-to-one association to UserRole
	@OneToMany(mappedBy="user", fetch=FetchType.EAGER)
	private Set<UserRole> userRoles;

	public User() {
	}

	public int getUserId() {
		return this.userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public byte getEnabled() {
		return this.enabled;
	}

	public void setEnabled(byte enabled) {
		this.enabled = enabled;
	}

	public String getMail() {
		return this.mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getNombre() {
		return this.nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Producto> getProductos() {
		return this.productos;
	}

	public void setProductos(Set<Producto> productos) {
		this.productos = productos;
	}

	public Set<UserRole> getUserRoles() {
		return this.userRoles;
	}

	public void setUserRoles(Set<UserRole> userRoles) {
		this.userRoles = userRoles;
	}

	public UserRole addUserRole(UserRole userRole) {
		Set<UserRole> roles = null;
		if(getUserRoles() == null){
			roles = new HashSet<UserRole>();
		}else{
			roles = getUserRoles();
		}
		roles.add(userRole);
		userRole.setUser(this);

		return userRole;
	}

	public UserRole removeUserRole(UserRole userRole) {
		getUserRoles().remove(userRole);
		userRole.setUser(null);

		return userRole;
	}
	
	public boolean hasProducto(Producto p){
		boolean has = false;
		for(Producto productoIterator : this.productos){
			if(productoIterator.getIdProductos() == p.getIdProductos()){
				has = true;
				break;
			}
		}
		return has;
	}

}